﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using ScriptEditor;
namespace LuxsanTool.Tool
{
    
    public class ScriptFactory
    {

        //public  frmScriptEditor m_frmScriptEditor = new frmScriptEditor();

        //public Other1BaseTool m_other1BaseTool = null;

        public frmScript m_frmScript = new frmScript();

        public string m_strStationName = "";
        public string m_strToolName = "";

        public string m_strScriptTxt = "";


        #region CreateInstanceWithDll 根据Dll创建实例
        /// <summary>
        /// 根据Dll创建实例
        /// </summary>
        /// <param name="_stationName"></param>
        /// <param name="_toolName"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public LuxsanTool.Tool.BaseTool CreateInstanceWithDll(string _strFilePath, string _stationName, string _toolName, out string strError)
        {
            //m_strStationName = _stationName;
            //m_strToolName = _toolName;
            frmScriptEditor m_frmScriptEditor = new frmScriptEditor();
            LuxsanTool.Tool.Other1BaseTool other1BaseTool = null;
            var CreateInstance = m_frmScriptEditor.CreateInstanceWithDll(_strFilePath, _stationName, _toolName, out strError);
            if (CreateInstance == null)
            {
                other1BaseTool = new Other1BaseTool(_toolName);
                other1BaseTool.m_executeStatus.bStatus = false;
                other1BaseTool.m_executeStatus.strErrorMsg = $"{_stationName}:{_toolName}:使用Dll创建Dll失败 {strError}";
                MessageBox.Show(other1BaseTool.m_executeStatus.strErrorMsg, "编译脚本");
            }
            else
            {
                other1BaseTool = CreateInstance as Other1BaseTool;
            }
            other1BaseTool.SetOther1BaseTool(_toolName);
            m_frmScriptEditor.Dispose();
            return other1BaseTool;
        }
        #endregion


        #region SaveTxt_Copy 保存脚本文件
        /// <summary>
        /// 保存脚本文件 不会改变 m_CsfilePath 值
        /// </summary>
        /// <param name="fileName"></param>
        //public bool SaveTxt_Copy(string _strFilePath, String _strStationName, string _strToolName, string _strData)
        //{
        //    frmScriptEditor m_frmScriptEditor = new frmScriptEditor();
        //    return m_frmScriptEditor.SaveTxt_Copy(_strFilePath, _strStationName, _strToolName, _strData);
        //}
        #endregion

        #region LoadTxt_Copy 加载脚本文件
        /// <summary>
        /// 加载脚本文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        //public string LoadTxt_Copy(string _strFilePath, String _strStationName, string _strToolName, out bool bdefault)
        //{
        //    bdefault = false;
        //    frmScriptEditor m_frmScriptEditor = new frmScriptEditor();
        //   return m_frmScriptEditor.LoadTxt_Copy(_strFilePath, _strStationName, _strToolName,out  bdefault);
        //}

        #endregion

        public void ShowScript(string _strFilePath,string _strStation, string _strTool)
        {
            frmScriptEditor m_frmScriptEditor = new frmScriptEditor();
            //m_frmScriptEditor.DisposeScriptEditor();
           string strError= m_frmScriptEditor.Initial(_strFilePath, _strStation, _strTool);
            if (strError != "")
            {
                MessageBox.Show(strError, "Warning", MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
                m_frmScriptEditor.Dispose();
                return;
            }
                m_frmScript.SetCap($"{_strStation}工站-{_strTool}工具");
                m_frmScript.AddScriptControl(m_frmScriptEditor);
                m_frmScript.WindowState = FormWindowState.Maximized;
                m_frmScript.ShowDialog();

               m_frmScriptEditor.Dispose();
        }



    }
}






